/**
 * 
 * @authors Riche
 * @date    2016-07-06 14:39:22
 * @version 1.1.0
 */
var questionID = $.getUrlVar("id");
var backUrl = $.getUrlVar("back");

var vm = new Vue({
    el: "#app",
    data: {
        title: '',
        typeID: '',
        addShow: false,
        addphotoshow: false,
        blocks: [],
        askTitle: '',
        askContent: '',
        contentPicture: []
    },
    compiled: function() {
        var _self = this;
        _self.loadPhoto(); //调用图片预加载


        // Common.ajaxCall({ url: "ask_getTypeList", datatype: 'json', type: 'get'}, function (data) {
        //     if(data.error_code==0){
        //         _self.blocks = data.data;
        //     }else{
        //         alert(data.msg);
        //     }
        // });

        // 选择图片
        document.getElementById('pic').onchange = function (e) {
            event = e || event;
            var img = event.target.files[0];
            var formData = new FormData();
            if (!img) {
                return;
            }
            // 判断图片格式  
            if (!(img.type.indexOf('image') == 0 && img.type && /\.(?:jpg|png|gif)$/.test(img.name))) {
                alert('图片只能是jpg,gif,png');
                return;
            }

            //定义图片参数
            var uploadFile = new FormData();
            uploadFile.append("uploadFile", img);
            //var uploadFile = new FormData(img);

            //接口方面的更新，现在上传图片必须得有accessToken等
            getaccessToken(function(token) {

                var url = 'ask_getFolderId';
                Common.ajaxCall({
                    url: url,
                    datatype: 'json',
                    type: 'post'
                }, function(data) {
                    if (data.error_code == 0) {
                        uploadFile.append("folderId", data.data[0].folderId);
                        uploadFile.append("accessToken", token);
                        uploadFile.append("tags", _self.title);

                        //data = Common.jsonEncode(data);
                        Common.ajaxSubmitPic({
                            url: 'upload_file',
                            data: uploadFile,
                        }, function(data) {
                            if (data.error_code == 0) {
                                //添加图片到内容中去
                                /*$('#content').htmlarea('tofocus');
                                $('#content').htmlarea('image', data.data[0].content);*/
                                var dom = '<li class="imgBox"><i class="closeImg" onclick="delImg(this);"></i><span class="photo j_1"><img data-src="'+data.data[0].content+'" data-fileid="'+data.data[0].fileId+'" class="loadImg"></span></li>';
                                $('#dom').append(dom);
                                //$('#pic1').attr('src',image);
                                _self.contentPicture.push(data.data[0].content);
                                _self.loadPhoto();
                                //vm.addReplyPic(image);
                            } else {
                                alert(data.msg);
                            }
                        });
                    } else {
                        alert(data.msg);
                    }
                });
            });
        }
    },
    methods: {
        publish: function() { //提交提问内容
            var _self = this;
            var content = $("#content").val();
            /*content = $('#content').htmlarea('html'); //取内容*/
            if (!content.trim()) { //如果没填写内容
                alert('请填写内容');
                $('#content').focus();
                return false;
            }

            var data = {
                questionID: questionID,
                content: content,
                contentPicture: _self.contentPicture.join(',')
            };
            //data = Common.jsonEncode(data);
            Common.ajaxCall({ url: 'ask_addAnswer', datatype: 'json', type: 'get', data: data }, function(data) {
                if (data.error_code == 0) {
                    alert(data.msg);
                    setTimeout(function() { //延迟跳转
                        window.location.href = decodeURIComponent(backUrl);
                    }, 500);
                } else {
                    alert(data.msg);
                }
            });

        },
        addphoto: function() {
            if (this.addphotoshow) {
                this.addphotoshow = false;
            } else {
                this.addphotoshow = true;
            }
        },
        loadPhoto: function() {
            myJS.imgScale('.j_1', '1', '1');
            myJS.imgScale('.j_1511', '15', '11');
            myJS.ImgFull('.loadImg', 'images/liby.png');
        },
        selectPic: function() {
            //选择图片
            $("#pic").click();
        },
        back: function() {
            window.location.href = decodeURIComponent(backUrl);
        }
    },
    watch: {

    }
});